package org.jiaxin.nutrient.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * @Author: jiaxin
 * @Description : TODO 获取不同类型的 通过JDBC访问的连接对象
 * @date: 2022/11/1 16:23
 **/

public class JdbcConnection {
    //Hive JDBC 连接库
    private static String hiveDriverName = "org.apache.hive.jdbc.HiveDriver";
    //SqlServer JDBC 连接类
    private static String sqlServerDriverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
    //Mysql 的JDBC连接类
    private static String mysqlDriverName="com.mysql.jdbc.Driver";


    /**
     *
     * @return Connection
     * @throws SQLException
     * TODO  获取 HiveJDBC连接
     */
    public static Connection getHiveJDBCConnection() throws SQLException {
        try {
            Class.forName(hiveDriverName);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.exit(1);
        }
        //return  DriverManager.getConnection("jdbc:hive2://islave4:10000","","");
        return DriverManager.getConnection("jdbc:hive2://islave1:2181,islave2:2181,islave3:2181/;serviceDiscoveryMode=zooKeeper;zooKeeperNamespace=hiveserver2","","");
    }


    /**
     *
     * @param userName
     * @param password
     * @param url
     * @return Connection
     * @throws SQLException
     * TODO 获取 SQLServer JDBC 连接
     */

    public static Connection getSQLServerConnection(String userName,String password,String url) throws SQLException {
        Connection connection=null;
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            connection=DriverManager.getConnection(url,userName,password);
            return connection;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.exit(1);
        }
        return  connection;
    }

    public static Connection getSQLConnection(String driver,String userName,String password,String url) throws SQLException {
        Connection connection=null;
        try {
            Class.forName(driver);
            connection = DriverManager.getConnection(url, userName, password);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.exit(1);
        }
        return  connection;
    }


    /**
     * TODO 获取Mysql的链接对象
     * @param userName
     * @param password
     * @param url
     * @return
     */
    public static Connection getMysqlConnection(String userName,String password,String url){
        Connection connection=null;
        try {
            Class.forName(mysqlDriverName);
            connection = DriverManager.getConnection(url, userName, password);
            return connection;
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
            System.exit(1);
        }

        return  connection;
    }


}
